package com.sxkj.entity.base;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sxkj.common.utils.IdUtils;
import org.hibernate.validator.constraints.Length;

/**
 * 数据Entity类
 */
//@Data
public abstract class DataEntity<T> extends BaseEntity<T> {

	private static final long serialVersionUID = 1L;

	protected String updateBy;// 更新者
	protected Integer updateTime;// 更新时间戳
	protected Integer delFlag;// 删除标记（0：正常；1：删除；2：审核）
	protected String createBy;// 创建者
	protected Integer createTime;// 创建时间戳
	protected String remarks;	// 备注

	public Integer now() {
		return (int) (System.currentTimeMillis() / 1000);
	}

	public String getCreateBy() {
		return createBy;
	}

	public void setCreateBy(String createBy) {
		this.createBy = createBy;
	}

	public Integer getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Integer createTime) {
		this.createTime = createTime;
	}

	public String getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(String updateBy) {
		this.updateBy = updateBy;
	}

	public Integer getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Integer updateTime) {
		this.updateTime = updateTime;
	}

	@JsonIgnore
	@Length(min=1, max=1)
	public Integer getDelFlag() {
		return this.delFlag;
	}

	public void setDelFlag(Integer defFlag) {
		this.delFlag = delFlag;
	}

//	public String uuid() {
//		return R.UU32().toLowerCase();
//	}

	@Length(min=0, max=255)
	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public DataEntity() {
		super();
		this.delFlag = DEL_FLAG_NORMAL;
	}

	public DataEntity(String id) {
		super(id);
	}

	/**
	 * 插入之前执行方法，需要手动调用
	 */
	@Override
	public void preInsert(){
		// 不限制ID为UUID，调用setIsNewRecord()使用自定义ID
		if (!this.isNewRecord){
//			setId(IdGen.nextId());
			setId(IdUtils.getRandomIdByUUID());
		}

//		User user = UserUtils.getUser();
//		if (StringUtils.isEmpty(user.getId())){
//			this.updateBy = user;
//			this.createBy = user;
//		}else{
//			user = new User();
//			user.setId("1");
//			this.updateBy = user;
//			this.createBy = user;
//		}
		this.delFlag = 0;
		this.createTime = this.updateTime = now();
	}

	/**
	 * 更新之前执行方法，需要手动调用
	 */
	@Override
	public void preUpdate(){
//		User user = UserUtils.getUser();
//		if (StringUtils.isNotBlank(user.getId())){
//			this.updateBy = user;
//		}else{
//			user = new User();
//			user.setId("1");
//			this.updateBy = user;
//		}
		this.updateTime = now();
	}


    //	@Override
//	public String toString() {
//		return String.format("/*%s*/%s", super.toString(), Json.toJson(this, JsonFormat.compact()));
//	}

}
